package com.cd2cd.code_template.service.impl;

import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.domain.SysUser;
import com.cd2cd.code_template.code_template.domain.gen.SysAuthCriteria;
import com.cd2cd.code_template.code_template.domain.gen.SysUserCriteria;
import com.cd2cd.code_template.code_template.mapper.SysAuthMapper;
import com.cd2cd.code_template.code_template.mapper.SysUserMapper;
import com.cd2cd.code_template.configuration.constants.AuthTypeEnum;
import com.cd2cd.code_template.configuration.constants.ServiceCode;
import com.cd2cd.code_template.configuration.security.TokenUser;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService;
import com.cd2cd.code_template.service.ConfigurationService;
import com.cd2cd.code_template.util.BeanUtil;
import com.cd2cd.code_template.util.CommUtils;
import com.cd2cd.code_template.util.Constants;
import com.cd2cd.code_template.vo.BaseRes;
import com.cd2cd.code_template.vo.SysAuthVo;
import com.cd2cd.code_template.vo.SysRouteVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class ConfigurationServiceImpl implements ConfigurationService {


    @Resource
    private SysUserMapper sysUserMapper;

    @Value("${system.login.account}")
    private String configUsername;

    @Resource
    private SysAuthMapper sysAuthMapper;


    @Override
    public BaseRes<SysUser> getLoginUserInfo() {

        BaseRes<SysUser> res = new BaseRes<>(ServiceCode.SUCCESS);

        TokenUser loginUserInfo = CommUtils.getLoginUserInfo();

        String type = loginUserInfo.getType();

        SysUser sysUser = new SysUser();

        // 配置后台用户信息
        if (type.equals(AuthTypeEnum.SysAdmin.name())) {
            sysUser.setUsername(configUsername);
            sysUser.setLoginType(AuthTypeEnum.SysAdmin.name());
        }else if (type.equals(AuthTypeEnum.Backend.name())){

            String username = loginUserInfo.getUsername();
            SysUserCriteria sysUserCriteria = new SysUserCriteria();
            sysUserCriteria.createCriteria().andUsernameEqualTo(username).andDelFlagEqualTo("ok");

            List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserCriteria);

            if (sysUsers.size() > 0) {
                sysUser = BeanUtil.voConvertIgnore(sysUsers.get(0), SysUser.class, "password");
            }

            sysUser.setLoginType(AuthTypeEnum.Backend.name());
        }

        res.setData(sysUser);

        return res;
    }


    @Override
    public BaseRes<List<SysRouteVo>> getMenuList() {

        BaseRes<List<SysRouteVo>> res = new BaseRes<>(ServiceCode.SUCCESS);

        TokenUser loginUserInfo = CommUtils.getLoginUserInfo();


        String username = loginUserInfo.getUsername();
        String type = loginUserInfo.getType();

        List<SysAuth> sysAuths;

        // 如果 admin 或 系统配置后台 则查处对应类型所有菜单
        if ("admin".equals(username) || LoadUserService.SysAdmin.class.getSimpleName().equals(type)) {
            sysAuths = sysAuthMapper.selectMenuByType(type);
        }else  {
            Integer userId = loginUserInfo.getUserId();
            sysAuths = sysAuthMapper.selectMenuByUser(userId,type);
        }

        List<SysAuthVo> sysAuthVos = BeanUtil.voConvertList(sysAuths, SysAuthVo.class);
        Collections.sort(sysAuthVos);

        List<SysRouteVo> menuNode = getMenuNode(sysAuthVos, 0);
        res.setData(menuNode);
        return res;
    }

    private List<SysRouteVo> getMenuNode(List<SysAuthVo> authList, Integer pid) {
        List<SysRouteVo> list = new ArrayList<>();

        for (SysAuthVo sysAuthVo : authList) {
            if (sysAuthVo.getPid().equals(pid)) {
                Integer id = sysAuthVo.getId();
                SysRouteVo sysRouteVo = new SysRouteVo();
                sysRouteVo.setName(sysAuthVo.getName());
                sysRouteVo.setIcon(sysAuthVo.getIcon());
                sysRouteVo.setPath(sysAuthVo.getRoutePath());

                List<SysRouteVo> sysAuthNode = getMenuNode(authList, id);
                if (sysAuthNode.size() > 0) {
                    sysRouteVo.setRoutes(sysAuthNode);

                }
                list.add(sysRouteVo);
            }
        }

        return list;
    }

}
